package com.example.demo.controller;

import com.example.demo.mapper.UserMapper;
import com.example.demo.vo.User;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class TestController {

    @Resource
    UserMapper userMapper;

    @RequestMapping("/selectUser")
    @ResponseBody
    public PageInfo<User> selectUser() {
        //1. 执行分页
        PageHelper.startPage(2, 5);
        //2. 执行查询
        List<User> users = userMapper.queryAllUsers();

        //3. 封装PageInfo对象
        PageInfo<User> pageInfo = new PageInfo<>(users);

        //4. 输出
        for (User user : pageInfo.getList()) {
            System.out.println(user);
        }

        return pageInfo;
    }

    @RequestMapping("/addUser")
    public String addUser() {
        List<User> list = new ArrayList<User>(){};
        for (int i = 0; i < 100; i++) {
            User user = new User();
            user.setBirth(new Date());
            user.setGender("男");
            user.setName("张三"+i);
            list.add(user);
        }
        userMapper.addAllUsers(list);
        System.out.println("test2");
        return "hello";
    }

    @RequestMapping("/delectUser")
    public String delectUser() {
        ArrayList<Integer> objects = new ArrayList<>();
        objects.add(18);
        objects.add(19);
        objects.add(20);
        userMapper.deleteUser(objects);
        return "hello";
    }

    @RequestMapping("/updateUser")
    public String updateUser() {
        User user = new User();
        user.setId(1);
        user.setName("里斯");
        user.setBirth(new Date());
        user.setGender("女");
        userMapper.updateUser(user);
        return "hello";
    }
}
